﻿//===============================================================================
// Apolineo Patterns & Practices LeanEngine Framework
//===============================================================================
// Copyright © Apolineo Desenvolvimento de Sistemas Ltda.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using LeanEngine.Entity;
using LeanEngine.Extensions;
using LeanEngine.Business.Security;
using LeanEngine.Business.Security.Entity;
using LeanEngine.Exception;

namespace LeanEngine.SalesConcept.Windows
{
    public partial class FormLogin : Form
    {
        public event Action<FormLogin> LoginSuccess;

        private const string ERR_USERNAME_MSG = "Informe o usuário.";
        private const string ERR_PASSWORD_MSG = "Informe a senha.";

        public FormLogin()
        {
            InitializeComponent();
        }

        //Propriedade que usamos para retornar o token de autenticação
        public Token SecureToken
        {
            get;
            private set;
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                string errorMessage = string.Empty;
                if (this.txtUsername.Text.IsNullOrEmpty())
                {
                    this.txtUsername.Focus();
                    errorProvider1.SetError(this.txtUsername, ERR_USERNAME_MSG);
                    errorMessage = "- ";
                    errorMessage += ERR_USERNAME_MSG;
                    errorMessage += Environment.NewLine;
                }

                if (this.txtPassword.Text.IsNullOrEmpty())
                {
                    this.txtPassword.Focus();
                    errorProvider1.SetError(this.txtPassword, ERR_PASSWORD_MSG);
                    errorMessage += "- ";
                    errorMessage += ERR_PASSWORD_MSG;
                }

                if (errorMessage.IsNullOrEmpty())
                {
                    try
                    {
                        //Aqui usamos o método LogOn (LeanEngine.Business.Security) para autenticar as
                        //credencias do usuario e gerar o token de acesso
                        SecureToken = BusinessSecurity.LogOn(this.txtUsername.Text.Trim(), this.txtPassword.Text.Trim());
                        if (SecureToken.IsNotNull() && LoginSuccess.IsNotNull()) LoginSuccess.Invoke(this);
                    }
                    catch (BusinessException ex)
                    {
                        MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    MessageBox.Show(errorMessage, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnLogin_Click(sender, null);
            }
        }
    }
}
